<!DOCTYPE html>
<html>
    <head>
	<meta charset="UTF-8">
        {include="style"}
		<style>
		.m-form{
		    padding: 25px;
		}
		.m-form input{
			margin:5px 0px;
		}
		</style>
    </head>
    <body>
        <div class="x-nav">
            <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>内链管理</cite></a>
             
              
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"  href="javascript:location.replace(location.href);" title="刷新"><i class="iconfont" style="line-height:30px">&#xe6aa;</i></a>
        </div>
        <div class="x-body">
        <div class="layui-card">
			<div class="layui-card-body">
			<xblock>
			{if(checkAction('Jzchain/delAll'))}	
			<a class="layui-btn layui-btn-danger layui-btn-sm" title="批量删除" onclick="delAll()">批量删除</a>
	        {/if}
		    {if(checkAction('Jzchain/addchain'))}
			<button type="button" class="layui-btn layui-btn-sm"  onclick="addchain()">添加内链</button>
		    {/if}
			
			
		
		    <table id="fieldtable" lay-filter="fieldtable"></table>
			<input id="select_data" type="hidden" value="" />

			</div>
		</div>
          
        </div>
		
		<script type="text/html" id="rightbar">
		 
		 {if(checkAction('Jzchain/editchain'))}	
			<a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="edit">修改</a>
		 {/if}
		 {if(checkAction('Jzchain/delAll'))}	
			<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
		 {/if}
		 
		
		</script>
		 <script type="text/html" id="isshowbar">
			<input type="checkbox" name="isshow" data-id="{{d.id}}"  lay-filter="sw"  {{#if(d.isshow=='显示'){ }}checked value="0" {{# }else{ }} value="1"{{# } }} value="1" lay-skin="switch">
		</script>
		 
       <script>
	   $(document).ready(function(){
			
	   
	   });
	   
	    layui.use(['laydate','element','table','form','laypage','layer'], function(){
                $ = layui.jquery;//jquery
             var  laydate = layui.laydate;//日期插件
             var  lement = layui.element;//面包导航
             var  layer = layui.layer;//弹出层
			 var  form = layui.form;
			 var  table = layui.table;
			
			
			  table.render({
				elem: '#fieldtable'
				,height: 500
				,url: '{fun U('Jzchain/index')}?ajax=1' //数据接口
				,page: false //开启分页
				,cols: [[ //表头
				  {field: 'id', title: 'ID', width:100}
				   ,{type:'checkbox'}
				  ,{field: 'title', title: '内链词', width:150}
				  ,{field: 'url', title: '内链', width:200}
				  ,{field: 'newtitle', title: '替换词'}
				  ,{field: 'num', title: '替换次数'}
				  ,{field: 'isshow', title: '显示',toolbar:"#isshowbar"}
				  ,{field: '', title: '操作',width:200, toolbar: '#rightbar', fixed:'right'}
				]]
			  });
			  
			  table.on('tool(fieldtable)', function(obj){
					var data = obj.data; //获得当前行数据
					var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
					var tr = obj.tr; //获得当前行 tr 的DOM对象
					 
					switch(layEvent){
					  case 'edit':
						var html = '<div class="m-form"><input type="hidden" id="id" value=\"'+data.id+'\">内链词<input type=\"text\" autocomplete="off" id= \"title\" value=\"'+data.title+'\" class=\"layui-input\" placeholder=\"内链词\"/>'
							+'替换词<input type=\"text\" value=\"'+data.newtitle+'\" id= \"newtitle\" autocomplete="off" class=\"layui-input\" placeholder=\"替换词，可为空！\"/>'
							+'内链<input type=\"text\" value=\"'+data.url+'\" id= \"url\" autocomplete="off" class=\"layui-input\" placeholder=\"请填写内链\"/>'
							+'替换次数<input type=\"text\" value=\"'+data.num+'\" id= \"num\" autocomplete="off" class=\"layui-input\" placeholder=\"替换次数，默认-1，替换全部，可不写。\"/>'
							+'<button type="button" style="margin-left: 45%;" class="layui layui-btn layui-btn-sm" onclick="update()">提交</button></div>';
							layer.open({
							  type: 1,
							  title:'修改内链',
							  skin: 'layui-layer-rim', //加上边框
							  area: ['800px', '400px'], //宽高
							  content: html
							});
							
					  break;
					 
					  case 'delete':
							layer.confirm('确认要删除吗？',function(index){
				
								$.post("{fun U('Jzchain/delchain')}",{id:data.id},function(r){
									var r = JSON.parse(r);
									if(r.code==0){
										layer.msg(r.msg,{icon: 1,time:1000},function(){
											table.reload('fieldtable', {
											  url: window.location.href+'?ajax=1'
											  ,where: {} 
											 
											});
										
										});
										
									}else{
										layer.msg(r.msg,{icon: 5,time:1000});
									}
								})
								
							
							   
							});
					  break;
					  
					  
					 
					}
				});
				table.on('checkbox(fieldtable)', function(obj){
				
				   var checkStatus = table.checkStatus('fieldtable'); //idTest 即为基础参数 id 对应的值
				   var len = checkStatus.data.length;
				   var arr=[];
				   if(len>0){
					for(var i=0;i<len;i++){
						arr.push(checkStatus.data[i].id);
					}
				   }
				   $("#select_data").val(arr);
				   
					
				});
			  
			  form.on('switch(sw)', function(data){
				
				
					var id = $(data.elem).attr('data-id');
					$.post('{fun U('changeStatus')}',{id:id,value:data.value},function(res){
					
					
					},'json')
					return false;
			  });
			  
			  
		});
		     function delAll () {
				
				var data = $("#select_data").val();
				if(data==''){
					alert('请选择对象！');return false;
				}
				layer.confirm('确认要删除吗？'+data,function(index){
					
					$.post("{fun U('Jzchain/delAll')}",{data:data},function(r){
					
							var r = JSON.parse(r);
							if(r.code==0){
							  
							  layer.msg('批量删除成功', {icon: 1,time:1000},function(){
								window.location.reload();
							  });
							  
							}else{
								
								layer.msg(r.msg,{icon: 5,time:1000});
							}
					})
					
					
					
				});
			  }
			function addchain(){
				var html = '<div class="m-form">内链词<input type=\"text\" autocomplete="off" id= \"title\" class=\"layui-input\" placeholder=\"内链词\"/>'
				+'替换词<input type=\"text\" id= \"newtitle\" autocomplete="off" class=\"layui-input\" placeholder=\"替换词，可为空！\"/>'
				+'内链<input type=\"text\" id= \"url\" autocomplete="off" class=\"layui-input\" placeholder=\"请填写内链\"/>'
				+'替换次数<input type=\"text\" id= \"num\" autocomplete="off" class=\"layui-input\" placeholder=\"替换次数，默认-1，替换全部，可不写。\"/>'
				+'<button type="button" style="margin-left: 45%;" class="layui layui-btn layui-btn-sm" onclick="tj()">提交</button></div>';
				layer.open({
				  type: 1,
				  title:'添加内链',
				  skin: 'layui-layer-rim', //加上边框
				  area: ['800px', '400px'], //宽高
				  content: html
				});
							
			
			
			
			}
			
			function tj(){
				 var title = $("#title").val();
				  var newtitle = $("#newtitle").val();
				  var url = $("#url").val();
				  var num = $("#num").val();
				  if(title=='' || url==''){
					layer.msg('内链词和内链不能为空！',{icon:5})
					return false;
				  }
				  $.post("{fun U('Jzchain/addchain')}",{title:title,newtitle:newtitle,url:url,num:num},function(res){
					if(res.code==0){
						layer.msg('新增成功！',{icon:6},function(){
							layer.closeAll();
						    location.reload();
						})
					}else{
						layer.msg(res.msg,{icon:5});
					}
				  },'json')
			}
			function update(){
				 var title = $("#title").val();
				 var id = $("#id").val();
				  var newtitle = $("#newtitle").val();
				  var url = $("#url").val();
				  var num = $("#num").val();
				  if(title=='' || url==''){
					layer.msg('内链词和内链不能为空！',{icon:5})
					return false;
				  }
				  $.post("{fun U('Jzchain/editchain')}",{id:id,title:title,newtitle:newtitle,url:url,num:num},function(res){
					if(res.code==0){
						layer.msg('修改成功！',{icon:6},function(){
							layer.closeAll();
						    location.reload();
						})
					}else{
						layer.msg(res.msg,{icon:5});
					}
				  },'json')
			}
			
		
		
			
			
       </script>
    </body>
</html>